package aiku.numericalcontroloa.Service.Impl;

import aiku.numericalcontroloa.Base.Utils.HttpStatus;
import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.RegionMapper;
import aiku.numericalcontroloa.Model.Entity.Region;
import aiku.numericalcontroloa.Service.RegionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.net.HttpCookie;
import java.util.List;

/**
 * 地区业务层
 * @Author 黄广庭
 * @Date 2024/5/10 17:37
 */
@Service
public class RegionServiceImpl implements RegionService {
    private final RegionMapper regionMapper;

    public RegionServiceImpl(RegionMapper regionMapper) {
        this.regionMapper = regionMapper;
    }


    @Override
    public Result getCascadeList(Region region) {
//        LambdaQueryWrapper<Region> regionLQW = new LambdaQueryWrapper<>();
//        // 条件搜索
//        if (region.getProvince() != null){
//            // 按省份名模糊查询
//            regionLQW.select(Region::getId,Region::getProvinceCode,Region::getProvince)
//                    .like(Region::getProvince,region.getProvince())
//                    .groupBy(Region::getProvinceCode);
//        }else if (region.getProvinceCode() != null && region.getCity() == null){
//            // 按省代码查询所有市
//            regionLQW.select(Region::getId,Region::getProvinceCode,Region::getProvince,Region::getCityCode,Region::getCity)
//                    .eq(Region::getProvinceCode,region.getProvinceCode())
//                    .groupBy(Region::getCityCode);
//        }else if (region.getProvinceCode() != null && region.getCity() != null){
//            // 按市名模糊查询
//            regionLQW.select(Region::getId,Region::getProvinceCode,Region::getProvince,Region::getCityCode,Region::getCity)
//                    .like(Region::getCity,region.getCity())
//                    .groupBy(Region::getCityCode);
//        }else if (region.getCityCode() != null  && region.getArea() == null){
//            // 按市代码查询所有区/县
//            regionLQW.eq(Region::getCityCode,region.getCityCode());
//        }else if (region.getCityCode() != null && region.getArea() != null){
//            // 按区/县名模糊查询
//            regionLQW.eq(Region::getCityCode,region.getCityCode())
//                    .like(Region::getArea,region.getArea());
//        }else{
//            // 无参数默认所有省
//            regionLQW.select(Region::getId,Region::getProvinceCode,Region::getProvince)
//                    .groupBy(Region::getProvinceCode);
//        }
//        List<Region> regionList = regionMapper.selectList(regionLQW);
        LambdaQueryWrapper<Region> regionLQW = new LambdaQueryWrapper<>();

// 条件搜索
        if (region.getProvince() != null) {
            // 按省份名模糊查询
            regionLQW.select(Region::getId, Region::getProvinceCode, Region::getProvince)
                    .like(Region::getProvince, region.getProvince())
                    .groupBy(Region::getProvinceCode);
        } else if (region.getProvinceCode() != null && region.getCity() == null) {
            // 按省代码查询所有市
            regionLQW.select(Region::getId, Region::getProvinceCode, Region::getProvince, Region::getCityCode, Region::getCity)
                    .eq(Region::getProvinceCode, region.getProvinceCode())
                    .groupBy(Region::getCityCode);
        } else if (region.getProvinceCode() != null && region.getCity() != null) {
            // 按市名模糊查询
            regionLQW.select(Region::getId, Region::getProvinceCode, Region::getProvince, Region::getCityCode, Region::getCity)
                    .like(Region::getCity, region.getCity())
                    .groupBy(Region::getCityCode);
        } else if (region.getCityCode() != null && region.getArea() == null) {
            // 按市代码查询所有区/县
            regionLQW.select(Region::getId, Region::getCityCode, Region::getCity) // 确保选择列符合你的要求
                    .eq(Region::getCityCode, region.getCityCode());
        } else if (region.getCityCode() != null && region.getArea() != null) {
            // 按区/县名模糊查询
            regionLQW.select(Region::getId, Region::getCityCode, Region::getCity, Region::getArea)
                    .eq(Region::getCityCode, region.getCityCode())
                    .like(Region::getArea, region.getArea());
        } else {
            // 无参数默认所有省
            regionLQW.select(Region::getId, Region::getProvinceCode, Region::getProvince)
                    .groupBy(Region::getProvinceCode);
        }

        List<Region> regionList = regionMapper.selectList(regionLQW);
        return Result.success("查询成功",regionList);
    }
}
